<template>
  <div class="index-lists">
    <el-card class="!border-none" shadow="never">
      <el-form ref="formRef" class="mb-[-16px]" :model="queryParams" :inline="true">
        <el-form-item label="创建时间" prop="createTime">
          <daterange-picker
              v-model:startTime="queryParams.createTimeStart"
              v-model:endTime="queryParams.createTimeEnd"
          />
        </el-form-item>
        <el-form-item label="合同名称" prop="contractName">
          <el-input class="w-[280px]" v-model="queryParams.contractName" />
        </el-form-item>
        <el-form-item label="客户名称" prop="customerName">
          <el-input class="w-[280px]" v-model="queryParams.customerName" />
        </el-form-item>
        <el-form-item label="乙方签约公司" prop="partybFirm">
          <el-select v-model="queryParams.partybFirm" class="w-[280px]" clearable>
            <el-option value="全部" />
            <el-option value="专利公司" />
            <el-option value="商标公司" />
            <el-option value="法律公司" />
          </el-select>
        </el-form-item>
        <el-form-item label="合同状态" prop="contractType">
          <el-select v-model="queryParams.contractType" class="w-[280px]" clearable>
            <el-option value="全部" />
            <el-option value="未开始" />
            <el-option value="执行中" />
            <el-option value="已完成" />
          </el-select>
        </el-form-item>
        <el-form-item label="合同类型" prop="contractType">
          <el-select v-model="queryParams.contractType" class="w-[280px]" clearable>
            <el-option value="代理合同" />
            <el-option value="直销合同" />
            <el-option value="其他" />
          </el-select>
        </el-form-item>
        <el-form-item label="金额" prop="amount">
          <el-input class="w-[280px]" v-model="queryParams.amount" />
        </el-form-item>
        <el-form-item label="创建人" prop="creator">
          <el-input class="w-[280px]" v-model="queryParams.creator" />
        </el-form-item>
        <el-form-item label="最后办理时间" prop="finalProcessingTime">
          <el-input class="w-[280px]" v-model="queryParams.finalProcessingTime" />
        </el-form-item>
        <el-form-item label="业务人员" prop="fieldStaff">
          <el-select v-model="queryParams.fieldStaff" class="w-[280px]" clearable>
            <el-option value="流程01" />
            <el-option value="营销02" />
            <el-option value="专利01" />
          </el-select>
        </el-form-item>
        <el-form-item label="对应商机" prop="correspondingBusiness">
          <el-input class="w-[280px]" v-model="queryParams.correspondingBusiness" />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="resetPage">查询</el-button>
          <el-button @click="resetParams">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="!border-none mt-4" shadow="never">
      <div>
        <!-- <el-button v-perms="['draft:add']" type="primary" @click="handleAdd()">
  <template #icon>
    <icon name="el-icon-Plus" />
  </template>
  新增
</el-button> -->
      </div>
      <el-table class="mt-4" size="large" v-loading="pager.loading" :data="localData" stripe>
        <el-table-column label="合同编号" prop="contractId" min-width="100" />
        <el-table-column label="合同名称" prop="contractName" min-width="100" />
        <el-table-column label="客户名称" prop="customerName" min-width="100" />
        <el-table-column label="乙方签约公司" prop="partybFirm" min-width="100" />
        <el-table-column label="合同类型" prop="contractType" min-width="100" />
        <el-table-column label="金额" prop="amount" min-width="100" />
        <el-table-column label="创建人" prop="creator" min-width="100" />
        <el-table-column label="最后办理时间" prop="finalProcessingTime" min-width="100" />
        <!-- <el-table-column label="操作" width="120" fixed="right">
  <template #default="{ row }">
    <el-button
      v-perms="['draft:edit']"
      type="primary"
      link
      @click="handleEdit(row)"
    >
      编辑
    </el-button>
    <el-button
      v-perms="['draft:del']"
      type="danger"
      link
      @click="handleDelete(row.id)"
    >
      删除
    </el-button>
  </template>
</el-table-column> -->
      </el-table>
      <div class="flex justify-end mt-4">
        <pagination v-model="pager" @change="getLists" />
      </div>
    </el-card>
    <edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false" />
  </div>
</template>
<script lang="ts" setup name="draft">
import { draftDelete, draftLists } from '@/api/draft'
import { usePaging } from '@/hooks/usePaging'
import feedback from '@/utils/feedback'
import { onMounted, ref } from 'vue';

// import EditPopup from "./edit.vue";
// const editRef = shallowRef<InstanceType<typeof EditPopup>>();
const showEdit = ref(false)
const queryParams = reactive({
  createTimeStart: '',
  createTimeEnd: '',
  contractId: '',
  contractName: '',
  customerName: '',
  partybFirm: '',
  contractType: '',
  amount: '',
  creator: '',
  finalProcessingTime: '',
  fieldStaff: '',
  fieldStaffId: '',
  correspondingBusiness: '',
  contractState: ''
})

const { pager, getLists, resetPage, resetParams } = usePaging({
  fetchFun: draftLists,
  params: queryParams
})

// 用于存储从本地缓存获取的数据
const localData = ref([]);

onMounted(() => {
  const cachedQueryParams = localStorage.getItem('queryParamsPage1');
  if (cachedQueryParams) {
    try {
      const parsedData = JSON.parse(cachedQueryParams);
      // 确保获取到的是数组
      if (Array.isArray(parsedData)) {
        localData.value = parsedData;
      }
    } catch (error) {
      console.error('解析本地存储数据时出错:', error);
    }
  }
})

// const handleAdd = async () => {
//   showEdit.value = true;
//   await nextTick();
//   editRef.value?.open("add");
// };

// const handleEdit = async (data: any) => {
//   showEdit.value = true;
//   await nextTick();
//   editRef.value?.open("edit");
//   editRef.value?.getDetail(data);
// };

const handleDelete = async (id: number) => {
  await feedback.confirm('确定要删除？')
  await draftDelete({ id })
  feedback.msgSuccess('删除成功')
  getLists()
}

getLists()
</script>